//
//  LGConsultRecordDescCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/18.
//  咨询记录 - 病情描述

#import "LGConsultRecordDescCell.h"

#define placeHolder  @"请输入..."

@interface LGConsultRecordDescCell()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UITextView *contentTF;

@end

@implementation LGConsultRecordDescCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.baseView];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.contentTF];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
        }];
        [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(8));
            make.left.equalTo(self.baseView).offset(viewPix(12));
            make.right.equalTo(self.baseView).offset(-viewPix(12));
            make.bottom.equalTo(self.baseView);
            make.height.equalTo(@(viewPix(67)));
        }];
    }
    return  self;
}

-(void)setContent:(NSString *)content{
    _content = content;
    if(content.length == 0 || [content isEqualToString:placeHolder]){
        self.contentTF.font = LGFont(14);
        self.contentTF.textColor = [UIColor colorWithString:@"#999999"];
        self.contentTF.attributedText = [placeHolder attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    }else{
        self.contentTF.font = LGFont(15);
        self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
        self.contentTF.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    }

    [self refreshContentHeight];
    [self performSelector:@selector(refreshContentHeight) withObject:nil afterDelay:0.1];
}

-(void)refreshContentHeight{
    CGSize maxSize = CGSizeMake(self.contentTF.size.width, MAXFLOAT);
    CGSize fitSize = [self.contentTF sizeThatFits:maxSize];
    CGFloat newHeight = fitSize.height;
    newHeight = (newHeight>=viewPix(67))?newHeight:viewPix(67);
    [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(newHeight));
    }];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

//病情描述文字输入
- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    if([self.contentTF.text isEqualToString:placeHolder]){
        self.contentTF.text = @"";
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    self.content = textView.text;
}

- (void)textViewDidChange:(UITextView *)textView{
    //键盘输入模式
    NSString *lang = textView.textInputMode.primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) {
            //没有高亮
            [self handleTextViewText:textView.text];
        }
    }else{
        [self handleTextViewText:textView.text];
    }
}

-(void)handleTextViewText:(NSString *)content{
    content = (content.length>100)?[content substringToIndex:100]:content;
    if(self.delegate && [self.delegate respondsToSelector:@selector(diseaseDescriptionChanged:)]){
        [self.delegate diseaseDescriptionChanged:content];
    }
    self.contentTF.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    CGSize maxSize = CGSizeMake(self.contentTF.size.width, MAXFLOAT);
    CGSize fitSize = [self.contentTF sizeThatFits:maxSize];
    CGFloat viewHeight = self.contentTF.bounds.size.height;
    CGFloat newHeight = fitSize.height;
    newHeight = (newHeight>=viewPix(67))?newHeight:viewPix(67);
    NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
    [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(newHeight));
    }];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}


-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"病情描述" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(12), viewPix(6), viewPix(100), viewPix(20));
    }
    return _titleLabel;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]init];
        _contentTF.textContainerInset = UIEdgeInsetsMake(viewPix(8), viewPix(8), viewPix(8), viewPix(8));
        _contentTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _contentTF.backgroundColor = [UIColor colorWithString:@"##FAFAFA"];
        _contentTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.scrollEnabled = NO;
        _contentTF.layer.borderWidth = 1;
        _contentTF.font = LGFont(14);
        _contentTF.cornerRidus = 5;
        _contentTF.delegate = self;
    }
    return _contentTF;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
